// Copyright 2020 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package mongo

import (
	"cgs-server/helper"
	"cgs-server/server/model"
	"context"
	"go.mongodb.org/mongo-driver/bson"
)

type users struct {
	db             *helper.Mongo
	collectionName string
}

func (u *users) Get(ctx context.Context, username string) (*model.User, error) {
	filter := bson.M{
		"Username": username,
		"$or": bson.A{
			bson.M{
				"isDeleted": 0,
			},
			bson.M{
				"isDeleted": nil,
			},
		},
	}
	result := &model.User{}
	find, err := u.db.FindOne("_User", filter, result)
	if err != nil {
		return nil, err
	}
	if find {
		return result, nil
	}
	return nil, nil
}

func newUsers(ds *datastore) *users {
	return &users{
		db: ds.db,
	}
}
